<style>
html,
body,
#app,
.login {
  height: 100%;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-form {
  width: 400px;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 40px 20px 0 0;
}
.el-form title {
  font-size: 16px;
}
</style>
<template>
  <div class="login">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <h3 class="title">登录</h3>
      <el-form-item label="账号" prop="userName">
        <el-input v-model="ruleForm.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      //登数据
      ruleForm: {
        userName: "",
        checkPass: ""
      },
      //验证规则
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 16, message: "长度在 3 到 16 个字符", trigger: "blur" }
        ],
        checkPass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          // alert("submit!");
          let _this = this;
          // this.axios.create({ headers: {'content-type': 'application/x-www-form-urlencoded'} })

          this.axios
            .post("/api/login", {
              // params: {
              userName: _this.ruleForm.userName,
              password: _this.ruleForm.checkPass
              // }
            })
            .then(res => {
              if (res.data.length) {
                // alert("登录成功");
                this.$store.commit('UPDATE_USERINFO',res.data[0])
                this.$message({
                  showClose: true,
                  message: "登录成功",
                  type: 'success'
                });
                _this.$router.push('/')
              } else {
                 this.$message({
                  showClose: true,
                  message: "登录失败",
                   type: 'error'
                });
              }
              console.log(res);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      // console.log(this.ruleForm.userName);
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

